package com.hc.dao.impl;

import com.hc.dao.ProvinceDao;
import com.hc.domain.Province;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("provinceDao")
public class ProvinceDaoImpl implements ProvinceDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Province province) {
        String sql = "insert into tb_province (name,area,priority,status) values (?,?,?,1)";
        Object[] params = {province.getName(), province.getArea(), province.getPriority()};
        int res = jdbcTemplate.update(sql, params);
        return res;
    }

    @Override
    public int update(Province province) {
        String sql = "update tb_province set name=?,area=?,priority=?,status=? where id =?";
        Object[] params = {province.getName(), province.getArea(), province.getPriority(), province.getStatus(), province.getId()};
        int res = jdbcTemplate.update(sql, params);
        return res;
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from  tb_province where id =?";
        int res = jdbcTemplate.update(sql, id);
        return res;
    }

    @Override
    public Province selectById(Integer id) {
        String sql = "select * from tb_province where id =?";
        Province res = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Province.class), id);
        return res;
    }

    @Override
    public long selectCount() {
        String sql = "select count(*) from tb_province ";
        long res = jdbcTemplate.queryForObject(sql, Long.class);
        return res;
    }

    @Override
    public List<Province> selectPage(Integer pageNum, Integer pageSize) {
        String sql = "select * from tb_province limit ?,?";
        List<Province> res = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Province.class), (pageNum - 1) * pageSize, pageSize);
        return res;
    }
}
